Skip to content

feat: Settings > Diagnostics shows which thread is using the CPU - #1038

Merged
badcuban merged 1 commit into
mainfrom
feat/cpu-by-thread
Nov 21, 2026
Merged

badcuban merged 1 commit into
mainfrom
feat/cpu-by-thread

Conversation

@badcuban

Copy link
Copy Markdown
Collaborator

Problem

Nothing in the app said which thread's agents were using the machine. Settings > Diagnostics listed the server's child processes with their CPU, but none were tied to a thread. On one Mac, agents ran a browser test suite about 600 times in a day at around 10 cores each, the battery drained at 50 to 60% an hour, and finding the cause took reading session logs.

Fix

A "CPU by thread" section at the top of Settings > Diagnostics.

  • A first line with how many of the machine's cores are busy, and how much of that is Threadlines and its agents.
  • One row per thread, one for Threadlines itself (the server and, in the desktop app, the app's own processes), and one for a provider whose single process serves all its threads.
  • Rows are sorted by the average over the last minute, so they don't trade places on every refresh. Columns: 1 min, now, memory.
  • A thread's name opens it. A row expands to its five busiest processes.

Whose a process is. Only from what the server already tracks, never from command lines or folders:

  • each driver's agent process ledger, which exists to prove a thread stopped. It gains owners(), drivers pass it up through ProviderAdapterShape.listProcesses, and ProviderService.listAgentProcesses gathers them. One driver that can't answer leaves out only its own processes.
  • each terminal's shell (TerminalManager.listShells).

A process belongs to the nearest tracked process above it. An agent that leads its own process group also keeps the group's processes that were handed to pid 1. Processes outside the server's tree that nobody tracks are left out.

Driver How it shows
Claude, Codex, Muse, Pi per thread, through the ledger
ACP agents (fx, Cursor, Antigravity, Grok, community) per thread, through the generic adapter's ledger
OpenCode one shared row: its one server runs every thread's work

CPU figures. What each process used since the read before, from ps time. ps pcpu is a recent average on macOS but an average over the process's whole life on Linux, where an agent that idled for an hour and then filled a core would read 2%. The POSIX process query gained a time= column for this. Windows already derives its figure from a running total.

No new background cost. The existing sampler runs only while someone asks and stops 2 minutes after the last request. It now hands each read to observers, and the new view is one. The page asks every 5 seconds only while the section is on screen and the window has been used in the last 5 minutes.

Guide. A new place (diagnostics.cpuByThread) and topic, so agents can answer "which thread is using my CPU".

Tests. Grouping: two agents in one thread, nested tracked agents, a child handed to pid 1, a tracked agent that left the server's tree, a reused group id, a shared provider, Windows rows without a group, and the app's own processes. Interval CPU: a long-lived process that turns busy, a reused pid, a gap, and rows without a total. A browser test checks the rows, the order by average, and that new instant values don't reorder rows. It fails when the sort uses the instant value. The full unit suite passes, with fmt, lint and typecheck. The browser suite passes except two tests in steppedLoops.browser.tsx, which this change doesn't touch; they failed at a load average of 23 and pass when the file runs alone.

Real run. On a throwaway server with a stand-in agent, a busy loop the agent started showed under its thread, and the server and its helper under Threadlines. That run was before the interval CPU change. Afterwards the new query, parser and interval figures were checked against this Mac's real process list and a real busy process.

Review. An independent review of the diff found eight problems. Seven are fixed: Linux lifetime averages, polling that never stopped in a focused window, a tracked agent outside the server's tree landing under the wrong thread, one snapshot counted twice in the average, one failing driver emptying every thread's row, a machine total that could exceed the core count shown, and ended processes still listed with CPU. Left as is: an agent's ledger entry outlives it when a child survives, so if those children die and the system reuses the number for a process that leads a group and then exits, the leftover group shows under the old thread. Fixing it means changing when the ledger forgets an entry, which the stop-confirmation path depends on.

Not checked. Linux and Windows. On Linux, ps counts CPU time in whole seconds, so one process's figure moves in 20% steps between 5-second reads; a group's sum and its one-minute average even that out. On Windows, a process that left its agent's tree isn't found.

Nothing in the app said which thread's agents were using the machine. The
Diagnostics page listed the server's child processes, but none were tied
to a thread.

A new "CPU by thread" section at the top of the page has one row per
thread, one for Threadlines itself, and one for a provider whose single
process serves all its threads (OpenCode). Rows are sorted by the average
over the last minute, a thread's name opens it, and a row expands to its
busiest processes.

Ownership comes from what the server already tracks: each driver's agent
process ledger (now listed through ProviderAdapterShape.listProcesses and
ProviderService.listAgentProcesses) and each terminal's shell. A process
belongs to the nearest tracked process above it, or to the process group
of an agent that leads one.

CPU is what each process used since the read before, from `ps time`.
`ps pcpu` is an average over a process's whole life on Linux, so the
POSIX process query gained a `time=` column.

It adds no timer: the existing on-demand sampler hands each read to the
new view, and the page asks only while the section is on screen and the
window is in use.
@vercel

vercel Bot commented Nov 21, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
threadlines Skipped Skipped Nov 21, 2026 8:03am UTC

Request Review

@badcuban
badcuban added this pull request to the merge queue Nov 21, 2026
Merged via the queue into main with commit a78c1c6 Nov 21, 2026
32 of 34 checks passed
@badcuban
badcuban deleted the feat/cpu-by-thread branch November 21, 2026 10:08

This branch was previously deployed

1 inactive deployment
Preview — 6ae917e3 Deployed Nov 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant